home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / siggraphCD / lib / libaux / aux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  9.2 KB  |  361 lines

  1. /*
  2.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <X11/Xlib.h>
  41. #include <X11/Xutil.h>
  42. #include <X11/keysym.h>
  43. #include <GL/glx.h>
  44. #include <GL/gl.h>
  45. #include "tk.h"
  46. #include "aux.h"
  47.  
  48. #if defined(__cplusplus) || defined(c_plusplus)
  49. #define class c_class
  50. #endif
  51.  
  52.  
  53. static struct {
  54.     int keyField;
  55.     void (*KeyFunc)(void);
  56. } keyTable[200];
  57.  
  58. static struct {
  59.     int mouseField;
  60.     void (*MouseFunc)(AUX_EVENTREC *);
  61. } mouseDownTable[20], mouseUpTable[20], mouseLocTable[20];
  62.  
  63. static int keyTableCount = 0;
  64. static int mouseDownTableCount = 0;
  65. static int mouseUpTableCount = 0;
  66. static int mouseLocTableCount = 0;
  67. static GLenum displayModeType = 0;
  68.  
  69.  
  70. static void DefaultHandleReshape(int w, int h)
  71. {
  72.     glViewport(0, 0, w, h);
  73.     glMatrixMode(GL_PROJECTION);
  74.     glLoadIdentity();
  75.     glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0);
  76.     glMatrixMode(GL_MODELVIEW);
  77.     glLoadIdentity();
  78. }
  79.  
  80. static void DefaultHandleExpose(int w, int h)
  81. {
  82. }
  83.  
  84. static GLenum MouseLoc(int x, int y, GLenum button)
  85. {
  86.     AUX_EVENTREC info;
  87.     GLenum flag;
  88.     int i;
  89.  
  90.     flag = GL_FALSE;
  91.     for (i = 0; i < mouseLocTableCount; i++) {
  92.     if ((button & AUX_LEFTBUTTON) == mouseLocTable[i].mouseField) {
  93.         info.event = AUX_MOUSELOC;
  94.         info.data[AUX_MOUSEX] = x;
  95.         info.data[AUX_MOUSEY] = y;
  96.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  97.         (*mouseLocTable[i].MouseFunc)(&info);
  98.         flag |= GL_TRUE;
  99.     }
  100.     if ((button & AUX_RIGHTBUTTON) == mouseLocTable[i].mouseField) {
  101.         info.event = AUX_MOUSELOC;
  102.         info.data[AUX_MOUSEX] = x;
  103.         info.data[AUX_MOUSEY] = y;
  104.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  105.         (*mouseLocTable[i].MouseFunc)(&info);
  106.         flag |= GL_TRUE;
  107.     }
  108.     if ((button & AUX_MIDDLEBUTTON) == mouseLocTable[i].mouseField) {
  109.         info.event = AUX_MOUSELOC;
  110.         info.data[AUX_MOUSEX] = x;
  111.         info.data[AUX_MOUSEY] = y;
  112.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  113.         (*mouseLocTable[i].MouseFunc)(&info);
  114.         flag |= GL_TRUE;
  115.     }
  116.     }
  117.     return flag;
  118. }
  119.  
  120. static GLenum MouseUp(int x, int y, GLenum button)
  121. {
  122.     AUX_EVENTREC info;
  123.     GLenum flag;
  124.     int i;
  125.  
  126.     flag = GL_FALSE;
  127.     for (i = 0; i < mouseUpTableCount; i++) {
  128.     if ((button & AUX_LEFTBUTTON) == mouseUpTable[i].mouseField) {
  129.         info.event = AUX_MOUSEUP;
  130.         info.data[AUX_MOUSEX] = x;
  131.         info.data[AUX_MOUSEY] = y;
  132.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  133.         (*mouseUpTable[i].MouseFunc)(&info);
  134.         flag |= GL_TRUE;
  135.     }
  136.     if ((button & AUX_RIGHTBUTTON) == mouseUpTable[i].mouseField) {
  137.         info.event = AUX_MOUSEUP;
  138.         info.data[AUX_MOUSEX] = x;
  139.         info.data[AUX_MOUSEY] = y;
  140.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  141.         (*mouseUpTable[i].MouseFunc)(&info);
  142.         flag |= GL_TRUE;
  143.     }
  144.     if ((button & AUX_MIDDLEBUTTON) == mouseUpTable[i].mouseField) {
  145.         info.event = AUX_MOUSEUP;
  146.         info.data[AUX_MOUSEX] = x;
  147.         info.data[AUX_MOUSEY] = y;
  148.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  149.         (*mouseUpTable[i].MouseFunc)(&info);
  150.         flag |= GL_TRUE;
  151.     }
  152.     }
  153.     return flag;
  154. }
  155.  
  156. static GLenum MouseDown(int x, int y, GLenum button)
  157. {
  158.     AUX_EVENTREC info;
  159.     GLenum flag;
  160.     int i;
  161.  
  162.     flag = GL_FALSE;
  163.     for (i = 0; i < mouseDownTableCount; i++) {
  164.     if ((button & AUX_LEFTBUTTON) == mouseDownTable[i].mouseField) {
  165.         info.event = AUX_MOUSEDOWN;
  166.         info.data[AUX_MOUSEX] = x;
  167.         info.data[AUX_MOUSEY] = y;
  168.         info.data[AUX_MOUSESTATUS] = AUX_LEFTBUTTON;
  169.         (*mouseDownTable[i].MouseFunc)(&info);
  170.         flag |= GL_TRUE;
  171.     }
  172.     if ((button & AUX_RIGHTBUTTON) == mouseDownTable[i].mouseField) {
  173.         info.event = AUX_MOUSEDOWN;
  174.         info.data[AUX_MOUSEX] = x;
  175.         info.data[AUX_MOUSEY] = y;
  176.         info.data[AUX_MOUSESTATUS] = AUX_RIGHTBUTTON;
  177.         (*mouseDownTable[i].MouseFunc)(&info);
  178.         flag |= GL_TRUE;
  179.     }
  180.     if ((button & AUX_MIDDLEBUTTON) == mouseDownTable[i].mouseField) {
  181.         info.event = AUX_MOUSEDOWN;
  182.         info.data[AUX_MOUSEX] = x;
  183.         info.data[AUX_MOUSEY] = y;
  184.         info.data[AUX_MOUSESTATUS] = AUX_MIDDLEBUTTON;
  185.         (*mouseDownTable[i].MouseFunc)(&info);
  186.         flag |= GL_TRUE;
  187.     }
  188.     }
  189.     return flag;
  190. }
  191.  
  192. static GLenum KeyDown(int key, GLenum status)
  193. {
  194.     GLenum flag;
  195.     int i;
  196.  
  197.     flag = GL_FALSE;
  198.     if (keyTableCount) {
  199.     for (i = 0; i < keyTableCount; i++) {
  200.         if (key == keyTable[i].keyField) {
  201.         (*keyTable[i].KeyFunc)();
  202.         flag |= GL_TRUE;
  203.         }
  204.     }
  205.     }
  206.     return flag;
  207. }
  208.  
  209. void auxExposeFunc(void (*Func)(int, int))
  210. {
  211.     tkExposeFunc(Func);
  212. }
  213.  
  214. void auxReshapeFunc(void (*Func)(int, int))
  215. {
  216.     tkExposeFunc(Func);
  217.     tkReshapeFunc(Func);
  218. }
  219.  
  220. void auxIdleFunc(void (*Func)(void))
  221. {
  222.     tkIdleFunc(Func);
  223. }
  224.  
  225. void auxKeyFunc(int key, void (*Func)(void))
  226. {
  227.     keyTable[keyTableCount].keyField = key;
  228.     keyTable[keyTableCount++].KeyFunc = Func;
  229. }
  230.  
  231. void auxMouseFunc(int mouse, int mode, void (*Func)(AUX_EVENTREC *))
  232. {
  233.     if (mode == AUX_MOUSEDOWN) {
  234.     mouseDownTable[mouseDownTableCount].mouseField = mouse;
  235.     mouseDownTable[mouseDownTableCount++].MouseFunc = Func;
  236.     } else if (mode == AUX_MOUSEUP) {
  237.     mouseUpTable[mouseUpTableCount].mouseField = mouse;
  238.     mouseUpTable[mouseUpTableCount++].MouseFunc = Func;
  239.     } else if (mode == AUX_MOUSELOC) {
  240.     mouseLocTable[mouseLocTableCount].mouseField = mouse;
  241.     mouseLocTable[mouseLocTableCount++].MouseFunc = Func;
  242.     } 
  243. }
  244.  
  245. void auxMainLoop(void (*Func)(void))
  246. {
  247.     tkDisplayFunc(Func);
  248.     tkExec();
  249. }
  250.  
  251. void auxInitPosition(int x, int y, int width, int height)
  252. {
  253.     tkInitPosition(x, y, width, height);
  254. }
  255.  
  256. void auxInitDisplayMode(GLenum type)
  257. {
  258.     displayModeType = type;
  259.     tkInitDisplayMode(type);
  260. }
  261.  
  262. GLenum auxInitWindow(char *title)
  263. {
  264.     int useDoubleAsSingle = 0;
  265.  
  266.     if (tkInitWindow(title) == GL_FALSE) {
  267.     if (AUX_WIND_IS_SINGLE(displayModeType)) {
  268.         tkInitDisplayMode(displayModeType|AUX_DOUBLE);
  269.         if (tkInitWindow(title) == GL_FALSE) {
  270.         return GL_FALSE;
  271.         }
  272.         fprintf(stderr, "Can't initialize a single buffer visual.\n");
  273.         fprintf(stderr, "Will use a double buffer visual instead,");
  274.         fprintf(stderr, "only drawing into the front buffer.\n");
  275.         displayModeType = displayModeType | AUX_DOUBLE;
  276.         useDoubleAsSingle = 1;
  277.     }
  278.     }
  279.     tkReshapeFunc(DefaultHandleReshape);
  280.     tkExposeFunc(DefaultHandleExpose);
  281.     tkMouseUpFunc(MouseUp);
  282.     tkMouseDownFunc(MouseDown);
  283.     tkMouseMoveFunc(MouseLoc);
  284.     tkKeyDownFunc(KeyDown);
  285.     auxKeyFunc(AUX_ESCAPE, auxQuit);
  286.     glClearColor(0.0, 0.0, 0.0, 1.0);
  287.     glClearIndex(0);
  288.     glLoadIdentity();
  289.     if (useDoubleAsSingle) {
  290.         glReadBuffer(GL_FRONT);
  291.     glDrawBuffer(GL_FRONT);
  292.     }
  293.     return GL_TRUE;
  294. }
  295.  
  296. void auxCloseWindow(void)
  297. {
  298.     tkCloseWindow();
  299.     keyTableCount = 0;
  300.     mouseDownTableCount = 0;
  301.     mouseUpTableCount = 0;
  302.     mouseLocTableCount = 0;
  303. }
  304.  
  305. void auxQuit(void)
  306. {
  307.     tkQuit();
  308. }
  309.  
  310. void auxSwapBuffers(void)
  311. {
  312.     tkSwapBuffers();
  313. }
  314.  
  315. Display *auxXDisplay(void)
  316. {
  317.     Display *ptr;
  318.     
  319.     tkGetSystem(TK_X_DISPLAY, (void *)ptr);
  320.     return ptr;
  321. }
  322.  
  323. Window auxXWindow(void)
  324. {
  325.     Window ptr;
  326.     
  327.     tkGetSystem(TK_X_DISPLAY, (void *)ptr);
  328.     return ptr;
  329. }
  330.  
  331. void auxSetOneColor(int index, float r, float g, float b)
  332. {
  333.     tkSetOneColor(index, r, g, b);
  334. }
  335.  
  336. void auxSetFogRamp(int density, int startIndex)
  337. {
  338.     tkSetFogRamp(density, startIndex);
  339. }
  340.  
  341. void auxSetGreyRamp(void)
  342. {
  343.     tkSetGreyRamp();
  344. }
  345.  
  346. void auxSetRGBMap(int size, float *rgb)
  347. {
  348.     tkSetRGBMap(size, rgb);
  349. }
  350.  
  351. int auxGetColorMapSize(void)
  352. {
  353.  
  354.     return tkGetColorMapSize();;
  355. }
  356.  
  357. void auxGetMouseLoc(int *x, int *y)
  358. {
  359.     tkGetMouseLoc(x, y);
  360. }
  361.